package com.mk.log.api;

import com.baomidou.mybatisplus.plugins.Page;
import com.mk.base.controller.BaseController;
import com.mk.base.model.response.RspPage;
import com.mk.common.excel.ExcelData;
import com.mk.common.excel.ExcelExportUtil;
import com.mk.log.bean.Log;
import com.mk.log.bean.LogRequest;
import com.mk.log.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 3:02 PM 3/13/18
 */
@RestController
@RequestMapping("/log")
public class LogApi extends BaseController {

    @Autowired
    private ILogService logService;

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public RspPage page(LogRequest request){
        Page<Log>  logPage = logService.page(request,getPagination(request));
        return convertSuccessPage(logPage);
    }

    @RequestMapping(value = "/export",method = RequestMethod.GET)
    public void exportExcelForLog(LogRequest request, HttpServletResponse response){
        try{
            //告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type","application/vnd.ms-excel");
            //下载文件的默认名称
            if(request != null && request.getLevel() != null && request.getLevel().intValue() == 1)
                response.setHeader("Content-Disposition","attachment;filename=" +
                        new String(("下载日志信息" + new SimpleDateFormat("yyyyMMdd")
                                .format(new Date()) + ".xls").getBytes(),"ISO-8859-1"));
            else
                response.setHeader("Content-Disposition","attachment;filename=" +
                        new String(("日志信息" + new SimpleDateFormat("yyyyMMdd")
                                .format(new Date()) + ".xls").getBytes(),"ISO-8859-1"));
            //编码
            response.setCharacterEncoding("UTF-8");
            List<Log> logs = logService.findByCols(request);

            ExcelExportUtil.exportExcel(response.getOutputStream(),setExcelDateFromLog(logs));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private ExcelData setExcelDateFromLog(List<Log> logs){
        ExcelData data = new ExcelData();
        data.setSheetName("sheet");
        List<String> titles = Arrays.asList(new String[]{"模块","说明","用户名","ip","时间"});
        List<String> columns = Arrays.asList(new String[]{"module","description","username","ip","createTime"});
        List<Map<String,Object>> filedList = new ArrayList<>();
        for (Log log : logs) {
            Map<String,Object> map = new HashMap<>();
            map.put("module",log.getModule());
            map.put("description",log.getDescription());
            map.put("username",log.getUsername());
            map.put("ip",log.getIp());
            map.put("createTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(log.getCreateTime()));
            filedList.add(map);
        }
        data.setTitles(titles);
        data.setColumns(columns);
        data.setRows(filedList);
        return data;
    }
}
